package com.lxk.controller;

import com.lxk.pojo.WxUser;
import com.lxk.service.UserService;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JohnSoybean
 * @create 2020/3/30 12:01
 * @description
 */
@Controller
public class UserController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService us;

    /**
     * 初始化用户列表页面
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/user")
    public String toUserPage(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = us.getTotalPages(num);
        }
        PageModel<WxUser> userInfo = us.selUserPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", userInfo);
        return "user";
    }

    /**
     * 查询某位用户
     * @param wxUserId
     * @return
     */
    @GetMapping("/user/query")
    @ResponseBody
    public Result<WxUser> getUserInfo(Integer wxUserId) {
        WxUser wxUser = us.selUserById(wxUserId);
        return Result.success(wxUser);
    }


    /**
     * 修改用户账户信息
     * @param wxUser
     * @return
     */
    @PostMapping("/user/modify")
    @ResponseBody
    public Result<Integer> modifyUserAccount(WxUser wxUser) {
        Integer upd = us.updUser(wxUser);
        return Result.success(upd);
    }

    /**
     * 删除用户
     * @param wxUserId
     * @return
     */
    @GetMapping("/user/delete")
    @ResponseBody
    public Result<Integer> deleteUser(Integer wxUserId) {
        Integer del = us.delUser(wxUserId);
        return Result.success(del);
    }


}
